package ua.kiev.tosik.price.modules.clients.models;

import ua.kiev.tosik.price.gui.models.SimpleTableModel;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;

import java.util.Vector;

public class PricesFilesPreviewTableModel extends SimpleTableModel {

	private Vector<Object[]> data;
	private ModuleConfig config;

	public PricesFilesPreviewTableModel(Vector<Object[]> data) {
		this.data = data;
		config = ModuleConfig.getInstance();
		colNames = new String[]{
			config.get("clients.files.tab.preview.filter.button.articleInternal.text"),
			config.get("clients.files.tab.preview.filter.button.article.text"),
			config.get("clients.files.tab.preview.filter.button.name.text"),
			config.get("clients.files.tab.preview.filter.button.price.text")
		};
	}

	public Vector<Object[]> getData() {
		return data;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	public void setColNames(String[] cols) {
		this.colNames = cols;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		try {
			return data.get(rowIndex)[columnIndex];
		} catch (Exception e) {
			// logger.error("Can't get data by index: " + columnIndex + ".\n" + data.get(rowIndex).length);
			return "";
		}
	}

	/*
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			Price price = data.get(rowIndex);
			switch (columnIndex) {
				case 0:
					return price.getArticle();
				case 1:
					return price.getName();
				case 2:
					return price.getPrice();
				default:
					return "";
			}
		} */

	public void removeRow(int idx) {
		data.remove(idx);
		fireTableRowsDeleted(idx, idx);
	}

	public void addRowSet(Vector<Object[]> data) {
		this.data.clear();
		this.data.addAll(data);
		fireTableDataChanged();
		logger.debug("Added " + this.data.size() + " records to price table");
	}

	public void addRowSet(int maxDataSize, Vector<Object[]> data) {
		this.data.clear();
		for ( int i = 0; i < ((data.size()> maxDataSize)?maxDataSize:data.size()); i++ ) {
			this.data.add(data.get(i));
		}
		fireTableDataChanged();
		logger.debug("Added " + this.data.size() + " records to price table");
	}

	public void addRow(Object[] rowData) {
		this.data.add(rowData);
		fireTableDataChanged();
	}

	public void addRow(int rowIndex, Object[] rowData) {
		this.data.add(rowIndex, rowData);
		fireTableRowsInserted(rowIndex, rowIndex);
	}

	public void updateRow(int rowIndex, Object[] rowData) {
		this.data.set(rowIndex, rowData);
		fireTableRowsUpdated(rowIndex, rowIndex);
	}

	public Object[] getRow(int index) {
		return data.get(index);
	}

}
